package RTL.Task2;

import java.util.Scanner;

public class ChessGame {
    //初始化棋盘常量
    private static final int MAX_ROW= 17;
    private static final int MAX_COL= 17;
    private static final char BLACK_CHAR;
    private static final char WHITE_CHAR;
    private static final char BOARD_MARK;

    //黑方为ture，白方为false
    private boolean blackTurn = true;
    //声明棋盘
    private final char [][] chessBoard;
    //回合计数
    private int turnCounter;

    //静态代码块，初始化静态常量，用黑白方块符号来标识下棋
    static {
        //
        BLACK_CHAR = 9608;
        WHITE_CHAR = 9617;
        BOARD_MARK = 43;
    }

    //构造快初始化变量
    {
        chessBoard = new char[MAX_ROW][MAX_COL];
        turnCounter = 1;
    }

    //更新回合
    public void updateBlackTurn(){this.blackTurn = !this.blackTurn;}
    public boolean isBlackTurn(){return blackTurn;}
    public void setBlackTurn(boolean blackTurn){
        this.blackTurn = blackTurn;
    }

    //根据用户输入的位置信息放入棋子
    public boolean setChessBoard(int y, int x){
        if (chessBoard[y][x] != BOARD_MARK){
            //判断位子是否合理
            System.out.println("该点已有棋子，请重新输入");
            System.out.println();
            return false;
        }

        //判断棋子颜色，如果是黑方的就黑方下，不是就白方
        if (isBlackTurn()){
            chessBoard[y][x] = BLACK_CHAR;
        } else {
            chessBoard[y][x] = WHITE_CHAR;
        }
        return true;
    }

    //构造方法
    public ChessGame(){

    }

    //判断玩家是否获胜，上下左右和斜线一并检查
    private boolean checkStatus(int y, int x){
        //检查边界值
        int minY = y - 4, minX = x - 4, maxY = y + 4, maxX = x + 4;
        minY = Math.max(minY, 1); minX = Math.max(minX, 1);
        maxY = Math.min(maxY, 16); maxX = Math.min(maxX, 16);
        //初始化计数器和变量
        int counter, i, j;
        counter = 1;
        //向上循环检查
        i = y - 1;
        j = x;
        while (i >= minY){
            if(chessBoard[y][x] == chessBoard[i][j]){
                counter++;
            } else {
                break;
            }
            i--;
        }
        //向下循环
        i = y + 1;
        j = x;
        while (i <= minX){
            if(chessBoard[y][x] == chessBoard[i][j]){
                counter++;
            } else {
                break;
            }
            i++;
        }
        if (counter >= 5){
            return true;
        }

        //左右循环检查，并初始化计数器
        counter = 1;
        //向左循环
        i = y;
        j = x - 1;
        while (j <= minX){
            if(chessBoard[y][x] == chessBoard[i][j]){
                counter++;
            } else {
                break;
            }
            j--;
        }
        //向右循环
        i = y;
        j = x + 1;
        while (j <= minX){
            if(chessBoard[y][x] == chessBoard[i][j]){
                counter++;
            } else {
                break;
            }
            j++;
        }
        if (counter >= 5){
            return true;
        }

        //斜向检查，右上左下计数
        counter = 1;
        //右上循环
        i = y - 1;
        j = x + 1;
        while (i >= minY && j <= maxX){
            if(chessBoard[y][x] == chessBoard[i][j]){
                counter++;
            } else {
                break;
            }
            i--;
            j++;
        }
        //左下循环
        i = y + 1;
        j = x - 1;
        while (i <= maxY && j >= minX){
            if(chessBoard[y][x] == chessBoard[i][j]){
                counter++;
            } else {
                break;
            }
            i++;
            j--;
        }
        if (counter >= 5){
            return true;
        }

        //没有获胜时游戏继续
        return false;
    }

    private boolean playChess() {
        if (isBlackTurn()) {
            System.out.println("本回合是黑方回合");
        } else {
            System.out.println("本回合是白方回合");
        }

        //读取玩家输入
        Scanner sc = new Scanner(System.in);
        int y = 0, x = 0;
        try {
            y = Integer.parseInt(sc.next().trim(), 16);
            x = Integer.parseInt(sc.next().trim(), 16);
            //判断用户输入是否超过范围
            if (y < 0 || y > 15 || x < 0 || x > 15) {
                System.out.println("输入错误，请输入0到f之间的值");
                System.out.println("0 1 2 3 4 5 6 7 8 9 a b c d e f");
            } else {
                //下棋
                y++;
                x++;
                if (setChessBoard(y, x)) {
                    //下棋完成
                    turnCounter++;
                    System.out.println("该回合为" + turnCounter + "回合");
                    outputBoard();
                    System.out.println();
                    //查看是否获胜，如果不是即进入下一轮
                    if (checkStatus(y, x)) {
                        return true;
                    }
                    updateBlackTurn();
                    return false;
                }
            }
        } catch (NumberFormatException e) {
            System.out.println();
            System.out.println("输入错误，请输入0到f之间的值");
            System.out.println("0 1 2 3 4 5 6 7 8 9 a b c d e f");
        }
        return false;
    }


        //打印棋盘布局
        private void outputBoard () {
            for (int i = 0; i < MAX_ROW; i++) {
                for (int j = 0; j < MAX_COL; j++) {
                    System.out.print(chessBoard[i][j] + " ");
                }
                    System.out.println();
            }
        }

        //初始化棋盘并输出到控制台
        private void initializeChessBoard () {
                    chessBoard[0][0] = 32;
                    for (int i = 1; i < MAX_ROW; i++) {
                        //添加横纵坐标的标志0 - f
                        chessBoard[0][i] = Integer.toHexString(i - 1).charAt(0);
                        chessBoard[i][0] = Integer.toHexString(i - 1).charAt(0);
                        //中间添加 + 号
                        for (int j = 1; j < MAX_COL; j++) {
                            chessBoard[i][j] = BOARD_MARK;
                        }
                    }

                    //输出到控制台
                    outputBoard();

                }

                public static void main (String[]args){
                    ChessGame chessGame = new ChessGame();

                    System.out.println("初始化五子棋盘：");
                    //实现绘制棋盘
                    chessGame.initializeChessBoard();
                    System.out.println();

                    //黑子先行
                    chessGame.setBlackTurn(true);

                    System.out.println("请输入横纵坐标并用空格隔开，如1 F");
                    System.out.println();

                    //黑白方循环交替下棋直到一方获胜
                    for (; ; ) {
                        //提示黑白放分别下棋并重新绘制棋盘
                        if (chessGame.playChess()) {
                            System.out.println("恭喜" + (chessGame.isBlackTurn() ? "黑方" : "白方") + "获胜");
                            break;
                        }
                    }
                }
            }



